package edu.usc.utils;

import java.io.BufferedReader;
import java.io.FileReader;

import weka.clusterers.ClusterEvaluation;
import weka.clusterers.DensityBasedClusterer;
import weka.clusterers.EM;
import weka.core.Instances;

public class WekaEM {

	public static void main(String[] args) {
		try {
		BufferedReader reader = new BufferedReader(
				new FileReader("temp_2_6_class.arff"));
		Instances data = new Instances(reader);
		reader.close();
		if (data.classIndex() == -1)
			   data.setClassIndex(data.numAttributes() - 1);
		
	    ClusterEvaluation eval;
	    String[] options;
	    DensityBasedClusterer  cl;

	    // normal
	    System.out.println("\n--> normal");
	    options    = new String[4];
	    options[0] = "-t";
	    options[1] = "temp_2_6_class.arff";
	    options[2] = "-I";
	    options[3] = "10";
	    	    
	    System.out.println(
	        ClusterEvaluation.evaluateClusterer(new EM(), options));
	    
	    // manual call
	    /*System.out.println("\n--> manual");
	    cl   = new EM();
	    ((EM)cl).setNumClusters(2);
	    ((EM)cl).setDebug(true);
	    System.out.println("Building");
	    cl.buildClusterer(data);
	    eval = new ClusterEvaluation();
	    eval.setClusterer(cl);
	    eval.evaluateClusterer(new Instances(data));
	    System.out.println("# of clusters: " 
	    + eval.getNumClusters());*/

	    // density based
	    System.out.println("\n--> density (CV)");
	    cl   = new EM();
	    eval = new ClusterEvaluation();
	    eval.setClusterer(cl);
	    eval.crossValidateModel(
	           cl, data, 10, data.getRandomNumberGenerator(1));
	    System.out.println("# of clusters: " + eval.getNumClusters());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
